using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security;
using System.Security.Cryptography;

namespace TBOCrypto
{
    public partial class Form1 : Form
    {
        XMLSupp xmlSup;
        byte[] digest;
        string hmac;

        public Form1()
        {
            InitializeComponent();
            xmlSup = new XMLSupp();
        }

        private void btnShowCerts_Click(object sender, EventArgs e)
        {
            txtDebug.Text = CertsMan.GetRootStoreInfo();
        }

        private void btnShowPersStoreInfo_Click(object sender, EventArgs e)
        {
            txtDebug.Text = CertsMan.GetPersonalStoreInfo();
        }

        private void btnAddCert_Click(object sender, EventArgs e)
        {
            string path = GetFileDialogPath();

            Password passForm = new Password();
            passForm.ShowDialog();
            string pass = passForm.txtPass.Text;

            CertsMan.AddCertToStore(path, pass);
        }

        private void RefreshStore()
        {
            txtDebug.Text = CertsMan.GetStoreInfo();
        }

        private void btnClearStore_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are You sure ??!!", "WARNING !!", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                CertsMan.ClearStore();
            }
        }

        private void btnSymEncode_Click(object sender, EventArgs e)
        {
            string path = GetFileDialogPath();

            NodeSelection nodeSel = new NodeSelection();
            nodeSel.LoadTree(xmlSup.LoadXmlFile(path));
            if (nodeSel.ShowDialog() == DialogResult.OK)
            {
                xmlSup.SymetricEncode(path, nodeSel.tvSelectEncNode.SelectedNode.Text);
            }

            
        }

        private string GetFileDialogPath()
        {
            OpenFileDialog ofDialog = new OpenFileDialog();
            ofDialog.ShowDialog();

            return ofDialog.FileName;
        }

        private void btnSymDecode_Click(object sender, EventArgs e)
        {
            string path = GetFileDialogPath();

            xmlSup.SymetricDecode(path);
        }

        private void btnAsymEnc_Click(object sender, EventArgs e)
        {
            string path = GetFileDialogPath();

            
            NodeSelection nodeSel = new NodeSelection();
            nodeSel.LoadTree(xmlSup.LoadXmlFile(path));
            if (nodeSel.ShowDialog() == DialogResult.OK)
            {
                xmlSup.AsymetricEncode(path, nodeSel.tvSelectEncNode.SelectedNode.Text);
            }
        }

        private void btnAsymDec_Click(object sender, EventArgs e)
        {
            string path = GetFileDialogPath();

            xmlSup.AsymetricDecode(path);
        }

        private void btnX509Enc_Click(object sender, EventArgs e)
        {
            string path = GetFileDialogPath();

            NodeSelection nodeSel = new NodeSelection();
            nodeSel.LoadTree(xmlSup.LoadXmlFile(path));
            if (nodeSel.ShowDialog() == DialogResult.OK)
            {
                xmlSup.Encode509(path, nodeSel.tvSelectEncNode.SelectedNode.Text);
            }
        }

        private void btnX509Dec_Click(object sender, EventArgs e)
        {
            string path = GetFileDialogPath();

            xmlSup.Decode509(path);
        }

        private void btnSign_Click(object sender, EventArgs e)
        {
            string path = GetFileDialogPath();

            xmlSup.SignXML(path);
        }

        private void btnEnvelope_Click(object sender, EventArgs e)
        {
            Encoding enc = Encoding.Unicode;
            txtDebug.Text = enc.GetString(AKSEnvelope.SignMsg(txtDebug.Text));
        }

        private void btnVerify_Click(object sender, EventArgs e)
        {
            try
            {
                Encoding enc = Encoding.Unicode;
                txtDebug.Text = enc.GetString(AKSEnvelope.DecodeMessage());
            }
            catch
            {
                txtDebug.Text = "Message was probably modified!";
            }
        }

        private void btnVerifyDigest_Click(object sender, EventArgs e)
        {
            if (XMLSupp.VerifyDigest(txtDebug.Text, digest))
                btnVerifyDigest.BackColor = Color.GreenYellow;
            else
                btnVerifyDigest.BackColor = Color.Red;
        }

        private void btnDigest_Click(object sender, EventArgs e)
        {
            digest = XMLSupp.ComputeDigest(txtDebug.Text);
            txtDigest.Text = Encoding.Unicode.GetString(digest);
        }

        private void btnRSACompute_Click(object sender, EventArgs e)
        {
            hmac = XMLSupp.GenerateHMAC(txtDebug.Text, txtPassword.Text);
            txtSignature.Text = hmac;
        }

        private void btnRSAVer_Click(object sender, EventArgs e)
        {
            if (XMLSupp.ValidateHMAC(txtDebug.Text, txtPassword.Text, hmac))
                btnRSAVer.BackColor = Color.GreenYellow;
            else
                btnRSAVer.BackColor = Color.Red;
        }
    }
}